import SendMsg from "@/api/SendMsg";
import {message} from "ant-design-vue";
import i18n from "../../../i18n/index";
import request from "@/util/request";
import store from "@/store";
import moment from "moment";


const ProductConfig = {
    namespaced: true,
    state: {
        // table数据
        dataSource: [],
        // 查询条件
        queryForm: {
            productName: '',
            productId: '',
        },
        loadTable: false,
        // table选中行
        selectRow: {},
        capForm: {
            productName: '',

        },
        productNameList: [],
        treeData: [{
            title: "Product ID",
            key: '0-0',
            children: []
        }],//树形控件的数据集
        selectedKeys: [],
        // 选中权限
        checkedKeys: [],
        // 展开节点
        expandedKeys: [],
        autoExpandParent: true,
        //Map图
        mapData: {},
        chartSpinning:false,
        productDisabled: false,
        aqlList: [],
    },
    mutations: {
        setChartSpinning(state,val){
            state.chartSpinning = val
        },
        // 查询prodId下拉框数据
        queryProductNameList(state) {
            let intrx = {
                trxId: "productConfig",
                actionFlg: "QPROD",
                productName: state.queryForm.productId
            };
            SendMsg.sendPostTrx(intrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    state.treeData[0].children = [];
                    outTrx.oaryB.sort((a,b) =>  a.productName.localeCompare(b.productName))
                    console.log(outTrx.oaryB)
                    if(outTrx.oaryB.length>0){
                        for (let i = 0; i <= outTrx.oaryB.length - 1; i++) {
                            state.treeData[0].children.push({
                                title: outTrx.oaryB[i].productName,
                                key: i,
                            })
                        }
                    }
                }
            }).finally(() => {
                state.loadTree = false
            });
        },

        //查询prodId相关数据
        queryProductInfo(state){
            state.dataSource = [];
            state.capForm = {
                productName: '',
            };
            state.mapData = {};
            if(!state.treeData[0].children[state.selectedKeys]){
                state.productDisabled = false
                return
            }
            let intrx = {
                trxId: "productConfig",
                actionFlg: "QPC",
                productName: state.treeData[0].children[state.selectedKeys].title,
            };
            state.chartSpinning = true
            SendMsg.sendPostTrx(intrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    if(outTrx.oaryA.length>0){
                        state.capForm = outTrx.oaryA[0];
                        if (outTrx.oaryA[0].klarfFileName) {
                            state.dataSource = outTrx.oaryA.map((item, index) => {
                                item.key = (index + 1);
                                return item;
                            });
                        }
                    }
                    state.mapData = {}
                    if(outTrx.oaryC){
                        state.mapData = outTrx.oaryC
                    }else{
                        state.chartSpinning = false
                    }

                    state.productDisabled = true
                }else{
                    //请求失败立即关闭loading,成功则在ProMap组件中绘图完成关闭loading
                    state.chartSpinning = false
                }
            }).catch(()=>{
                state.chartSpinning = false
            })
        },

        //delete prod
        deleteData(state){
            if(!state.capForm.productName){
                message.warn(i18n.t('menuItem.prodIdWarn'))
                return
            }
            let intrx = {
                trxId: "productConfig",
                actionFlg: "D",
                productName: state.capForm.productName,
            };
            SendMsg.sendPostTrx(intrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    message.success(i18n.t('menuItem.delSuccess'))
                    ProductConfig.mutations.cleanAll(state)
                }
            }).then(response => {
                ProductConfig.mutations.queryProductNameList(state)
            })
        },

        //保存Product Info
        saveProductNameInfo(state){
            let actionFlg;
            if(state.productDisabled == true){
                actionFlg = 'S'
            }else {
                actionFlg = 'A'
            }

            let intrx = {
                trxId: "productConfig",
                actionFlg: actionFlg,
                productName: state.capForm.productName,
                aqlStandard: state.capForm.aqlStandard,
                inspectionDie: state.capForm.inspectionDie,
                grossDie: state.capForm.grossDie,
            };
            SendMsg.sendPostTrx(intrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    message.success(i18n.t('menuItem.insertSuccess'))
                    ProductConfig.mutations.cleanAll(state)
                }
            }).then(response => {
                ProductConfig.mutations.queryProductNameList(state)
            })
        },
        // 清空弹窗数据
        cleanAll(state) {
            state.dataSource = [],
            state.queryForm = {
                productName: '',
                productId: '',
            },
            state.capForm = {

            },
            state.productNameList = [],
            state.treeData =  [{
                title: "Product ID",
                key: '0-0',
                children: []
            }],
            state.selectedKeys = [],
            state.mapData = {}
        },
        // 设置选中节点
        setCheckedKeys(state, val) {
            state.checkedKeys = val;
        },
        setSelectedKeys(state, val) {
            state.selectedKeys = val;
        },
        // 设置展开节点
        setExpandedKeys(state, val) {
            state.expandedKeys = val;
            state.autoExpandParent = false;
        },
        //表格行数据
        customRow(state, obj) {
            state.selectRow = {
                fileName: obj.klarfFileName,
                filePath: obj.klarfUploadUrl,
            }
        },
        //下载文件
        downloadFile(state){
            if(!state.selectRow.fileName){
                message.warn(i18n.t('menuItem.KlarfFileNameWran'))
                return
            }
            let inTrx = {
                fileName: state.selectRow.fileName,
                // filePath: state.selectRow.filePath
                productName: state.capForm.productName
            };
            inTrx.fabIdFk = window.localStorage.getItem("fabIdMes");
            inTrx.evtUsr = window.localStorage.getItem("userIdMes");
            return request({
                url:  `/FileDownloader/download`,
                method: "post",
                data: {
                    // fileName: inTrx.fileName,
                    // filePath: inTrx.filePath
                    productName: inTrx.productName
                },
                responseType: 'blob',
            }).then(response => {
                const blob = new Blob([response.data]); // 创建一个Blob对象，存储从后端获取的文件内容
                const link = document.createElement('a');
                link.href = URL.createObjectURL(blob); // 创建一个临时的URL链接用于下载
                link.download = inTrx.fileName; // 设置下载文件的名称
                link.click(); // 模拟点击下载链接
                URL.revokeObjectURL(link.href); // 释放URL对象，防止内存泄漏
            })

        },
        //删除文件
        deleteFile(state){
            if(!state.selectRow.fileName){
                message.warn(i18n.t('menuItem.KlarfFileNameWran'))
                return
            }
            let inTrx = {
                fileName: state.selectRow.fileName,
                productName: state.capForm.productName
            };
            inTrx.fabIdFk = window.localStorage.getItem("fabIdMes");
            inTrx.evtUsr = window.localStorage.getItem("userIdMes");
            request({
                url:  `/FileDelete/delete`,
                method: "post",
                data: {
                    fileName: inTrx.fileName,
                    userName: inTrx.evtUsr,
                    productName: inTrx.productName
                },
            }).then(response => {
                message.success(i18n.t('menuItem.delSuccess'))
                state.dataSource = []
                ProductConfig.mutations.queryProductInfo(state)
            })

        },
        // 查询prodId下拉框数据
        queryAqlStanrdList(state) {
            let intrx = {
                trxId: 'bisData',
                actionFlg: 'Q',
                iary: [{
                    dataCate: 'AQLD'
                }]
            }
            state.aqlList = []
            SendMsg.sendPostTrx(intrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode && outTrx.oary.length > 0) {
                    let oary = outTrx.oary
                    for (let i = 0; i <= oary.length - 1; i++) {
                        state.aqlList.push({
                            value: oary[i].dataDesc,
                            desc: oary[i].dataDesc + '--[' + oary[i].ext1 + '~' + oary[i].ext2 + ']'
                        })
                    }
                }
            })
        },
    },
    getters: {},
    action: {}
}
export default ProductConfig;
